/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package janelas;

import Classes.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractListModel;

/**
 *
 * @author Daniela
 */
public class PesquisarUsuario extends javax.swing.JFrame {

   private ContaDoUsuario janelaAnterior;
   private Sistema sistema;
   
    public PesquisarUsuario(ContaDoUsuario janelaAnterior) {
        initComponents();
        this.janelaAnterior = janelaAnterior;
        setResizable(false); 
        setLocationRelativeTo(null);
        sistema = janelaAnterior.getSistema();
        
    }
    
      
    public Sistema getSistema() { 
            return janelaAnterior.getSistema();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        labelPesquisar = new javax.swing.JLabel();
        campoPesquisar1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        listaUsuarios = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(204, 204, 255));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel2.setText("Você está pesquisando um usuário");

        labelPesquisar.setIcon(new javax.swing.ImageIcon("C:\\Users\\Daniela\\Desktop\\Icons\\buscar.gif")); // NOI18N
        labelPesquisar.setText("Pesquisar:");

        campoPesquisar1.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                campoPesquisar1CaretUpdate(evt);
            }
        });

        listaUsuarios.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listaUsuariosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(listaUsuarios);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(labelPesquisar)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(campoPesquisar1, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jLabel2)))
                .addContainerGap(33, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPesquisar)
                    .addComponent(campoPesquisar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(13, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void campoPesquisar1CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_campoPesquisar1CaretUpdate
       String chave = campoPesquisar1.getText();
        final List<String> listaDeUsuarios = new ArrayList<>();
        for (Usuario u : janelaAnterior.getSistema().getLista()) {
            if (u.getLogin().toLowerCase().contains(chave.toLowerCase()) && !u.getLogin().equals(janelaAnterior.getSistema().getUserName()))
                
                listaDeUsuarios.add(u.getLogin());
        }
        preencheUsuarios(listaDeUsuarios);
    }//GEN-LAST:event_campoPesquisar1CaretUpdate

    private void listaUsuariosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listaUsuariosMouseClicked
      Object valor = listaUsuarios.getSelectedValue();
        if (valor != null) { 
            String login = (String) valor;
            Usuario u = janelaAnterior.getSistema().procuraUsuario(login);
            new JanelaUsuario(this, u).setVisible(true);
        }
        this.setEnabled(false);
    }//GEN-LAST:event_listaUsuariosMouseClicked
   
     public void preencheUsuarios() { 
        final List<String> listaDeUsuarios = new ArrayList<>();
        for (Usuario u : janelaAnterior.getSistema().getLista()) {
            if (!u.getLogin().equals(janelaAnterior.getSistema().getUserName()))
                listaDeUsuarios.add(u.getLogin());
        }
        preencheUsuarios(listaDeUsuarios);
    }
    
    public void preencheUsuarios(final List<String> nomes) {
       listaUsuarios.setModel(new AbstractListModel() {
            public int getSize() { return nomes.size(); }
            public Object getElementAt(int i) { return nomes.get(i); }
        }); 
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField campoPesquisar1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelPesquisar;
    private javax.swing.JList listaUsuarios;
    // End of variables declaration//GEN-END:variables
}
